package com.basemanage.controller.staff;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import com.basemanage.core.controller.JCBaseController;
import com.basemanage.core.model.Condition;
import com.basemanage.core.model.Operators;
import com.basemanage.core.util.CommonUtils;
import com.basemanage.core.util.DateUtils;
import com.basemanage.core.util.JqGridModelUtils;
import com.basemanage.core.view.InvokeResult;
import com.basemanage.model.SmStaff;
import com.basemanage.service.StaffService;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.xiaoleilu.hutool.json.JSONObject;

public class StaffController extends JCBaseController {
	
	public void index() {
		render("staff_index.html");
	}
	
	/**
	 * 跳转（添加/编辑）页面
	 */
	public void addStaff(){
		Integer id=getParaToInt("id");
		String action="add";
		if(id!=null){
			this.setAttr("item", StaffService.dao.findById(id));
			action="edit";
		}
		this.setAttr("action", action);
		render("staff_add.html");
	}
	
	
	/**
	 * 数据分页列表
	 */
	public void getListData() {
		
		String name=this.getPara("name");
		Set<Condition> conditions=new HashSet<Condition>();
		if(CommonUtils.isNotEmpty(name)){
			conditions.add(new Condition("remark",Operators.LIKE,name));
		}
		
		Page<SmStaff> pageInfo= StaffService.dao.getPage(this.getPage(), this.getRows(),conditions,this.getOrderby());
		this.renderJson(JqGridModelUtils.toJqGridView(pageInfo)); 
	}
	
	/**
	 * 保存数据
	 */
	public void saveStaff(){
		SmStaff staff= getModel(SmStaff.class,"obj");
		InvokeResult result=  StaffService.me.saveStaff(staff);
		this.renderJson(result);
	}
	 
	/**
	 * 删除数据
	 */
	public void delStaff(){
		String ids=this.getPara("ids");
		InvokeResult result= StaffService.me.delStaff(ids);
		this.renderJson(result);
	}
	
	/*
	 * 管理员状态变更
	 */
	public void changeAdmin(){
		
		String id=this.getPara("changeid");
		System.out.print(id);
		InvokeResult result = StaffService.me.changeAdmin(id);
		this.renderJson(result);
		
	}
	/*
	 * 上传图片
	 */
	public void uploadimg() {

	    UploadFile uploadFile = getFile();//在磁盘上保存文件

	    JSONObject data = new JSONObject();

	    StringBuffer URL= getRequest().getRequestURL();
	    String prefixString = URL.substring(0, 21);

        if (uploadFile != null) {
            String fileName = uploadFile.getFileName();
            String extentionName = fileName.substring(fileName
                    .lastIndexOf(".")); // 后缀名
            System.out.print(prefixString);
            String newName =  prefixString + "/uploadfile/" + fileName;// 新名
//            String filePath = PathKit.getWebRootPath() + PropKit.get("uploadPath") + "/" + newName;// 文件完整路径
            uploadFile.getFile().renameTo(new File(newName)); // 重命名并上传文件

            //返回任意数据即代表上传成功
            data.put("filePath", newName);
            data.put("status", "success");
            renderJson(InvokeResult.success(data));
        }else {
            //返回任意数据即代表上传成功
        	data.put("error", "未选择文件");
        	data.put("status", "error");
            renderJson(InvokeResult.success(data));
        }
		
	}
}